package org.gangcai.mac.shop.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.DynamicBean;
import org.gangcai.mac.shop.bean.HomeIndexBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.util.SpannableStringUtils;
import org.gangcai.mac.shop.util.ToastUtils;
import org.gangcai.mac.shop.util.UtilsTool;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 文 件 名: PullToRefreshAdapter
 * 创 建 人: Allen
 * 创建日期: 16/12/24 19:55
 * 邮   箱: AllenCoder@126.com
 * 修改时间：
 * 修改备注：
 */
public class PullToRefreshAdapter extends BaseQuickAdapter<DynamicBean.DynamicInfoBean, BaseViewHolder> {
    private Context context;
    private ShareDynamicClickListener mShareDynamicClickListener;
    public PullToRefreshAdapter() {
        super( R.layout.layout_animation, null );
    }

    public PullToRefreshAdapter(Context context) {
        super( R.layout.layout_animation_01, null );
        this.context = context;
    }

    public PullToRefreshAdapter(Context context, ShareDynamicClickListener shareDynamicClickListener) {
        super( R.layout.layout_animation_01, null );
        this.context = context;
        this.mShareDynamicClickListener = shareDynamicClickListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, final DynamicBean.DynamicInfoBean item) {
        SpannableString spannableString = new SpannableString("姝雅瑞:"+item.getContent());
        spannableString.setSpan(new ForegroundColorSpan( Color.parseColor("#ed145b")), 0,4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText( R.id.tweetText, spannableString );
        List<DynamicBean.ImgInfo> img_list = item.getImg_list();
        if (this.context != null && img_list.size() > 0) {
            int imgID = R.id.img_01;
            if(img_list.size()==4){
                helper.setVisible(R.id.dynamicLayout_01,false);
                helper.setVisible(R.id.dynamicLayout_02,true);
                int imgID1 = R.id.img_11;
                for (int i = 0; i < 4; i++) {
                    ImageView imageView = (ImageView) helper.getView( imgID1 + i );
                    imageView.setVisibility( View.VISIBLE );
                    String img_path = img_list.get( i ).getImg_path();
                    Glide.with( this.context ).load( Constant.IMAGE_PATHS + img_path ).into( imageView );
                }
            }else{
                helper.setVisible(R.id.dynamicLayout_01,true);
                helper.setVisible(R.id.dynamicLayout_02,false);
                for (int i = 0; i < 9; i++) {
                    ImageView imageView = (ImageView) helper.getView( imgID + i );
                    if (i < img_list.size()) {
                        imageView.setVisibility( View.VISIBLE );
                        String img_path = img_list.get( i ).getImg_path();
                        Glide.with( this.context ).load( Constant.IMAGE_PATHS + img_path ).into( imageView );
                    } else {
                        imageView.setVisibility( View.GONE );
                    }
                }
            }
            helper.setOnClickListener(R.id.dynamic_shareBtn,new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mShareDynamicClickListener != null){
                        mShareDynamicClickListener.shareDynamicWithComment(item);
                    }
                }
            });
        }
    }

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ToastUtils.showShortToast( "事件触发了 landscapes and nedes" );
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor( context.getResources().getColor( R.color.clickspan_color ) );
            ds.setUnderlineText( true );
        }
    };

    public interface ShareDynamicClickListener {
        void shareDynamicWithComment(DynamicBean.DynamicInfoBean item);
    }

}
